package stack_and_queue;

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {

    private Queue<PetEnterQueue> dogQueue;
    private Queue<PetEnterQueue> catQueue;
    private long count;

    public DogCatQueue() {
        this.dogQueue = new LinkedList<PetEnterQueue>();
        this.catQueue = new LinkedList<PetEnterQueue>();
        this.count = 0;
    }

    public void add(Pet pet){
        if(pet.getType().equals("dog")){
            dogQueue.add(new PetEnterQueue(pet, this.count++));
        }else if(pet.getType().equals("cat")){
            catQueue.add(new PetEnterQueue(pet, this.count++));
        }else {
            throw new RuntimeException("err, not dog or cat.");
        }
    }

    public Pet pollAll(){
        if(!dogQueue.isEmpty() && !catQueue.isEmpty()){
            if(dogQueue.peek().getCount() < catQueue.peek().getCount()){
                return catQueue.poll().getPet();
            }else{
                return dogQueue.poll().getPet();
            }
        }else if(!dogQueue.isEmpty()){
            return dogQueue.poll().getPet();
        }else if(!catQueue.isEmpty()){
            return catQueue.poll().getPet();
        }else{
            throw new RuntimeException("the queue is empty");
        }
    }

    public Pet pollCat(){
        if(!catQueue.isEmpty()){
            return catQueue.poll().getPet();
        }else{
            throw new RuntimeException("this queue is empty.");
        }
    }

    public Pet pollDog(){
        if(!dogQueue.isEmpty()){
            return dogQueue.poll().getPet();
        }else{
            throw new RuntimeException("this queue is empty.");
        }
    }

    public boolean isEmpty(){
        return (dogQueue.isEmpty() && catQueue.isEmpty());
    }

    public boolean isDogEmpty(){
        return dogQueue.isEmpty();
    }

    public boolean isCatEmpty(){
        return catQueue.isEmpty();
    }
}
